package com.ssm.mssm.web.handler;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.common.ThisSystemException;
import com.ssm.mssm.entity.InformationEntity;
import com.ssm.mssm.function.InformationFunction;
import com.ssm.mssm.web.handler.ao.InformationAo;
import com.ssm.mssm.web.handler.vo.InformationVo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Reader;
import java.util.*;

import static com.ssm.common.ThisSystemUtil.$;
import static com.ssm.common.ThisSystemUtil.assertEquals;

/**
 * Created by asus on 2017/7/14.
 */
@Controller
public class InformationHandler {
    private static SqlSessionFactory sqlSessionFactory;
    private static Reader reader;
    static {
        try {
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Autowired
    InformationFunction fun;

    @RequestMapping(path = "/Information/qry.do",method= RequestMethod.GET)
    public String qryView(int pn,String string,HttpServletRequest request) throws Exception{
        ArrayList<InformationVo> lv=new ArrayList<>();
        List<InformationVo> plist=new ArrayList<>();
        int j=0;
        if("管理员".equals(string)){
            PageHelper.startPage(pn==0?1:pn,7);
            List<InformationEntity> list=fun.queryInformation(string);
            PageInfo pageInfo=new PageInfo(list);
            int pages=pageInfo.getPages();
            int pageSize=pageInfo.getPageSize();
            int lastpage=pageInfo.getLastPage();
            int[] navigatepagenums=pageInfo.getNavigatepageNums();
            int pagenum=pageInfo.getPageNum();
            request.setAttribute("pages",pages);
            request.setAttribute("pageSize",pageSize);
            request.setAttribute("pn",pn);
            request.setAttribute("lastpage",lastpage);
            request.setAttribute("pagenum",pagenum);
            request.setAttribute("navigatepagenums",navigatepagenums);
            for(InformationEntity i:list){
                InformationVo iv=new InformationVo();
                iv.setId(i.getId());
                iv.setUsername(i.getUsername());
                iv.setSex(i.getSex());
                iv.setQq(i.getQq());
                iv.setEmail(i.getEmail());
                iv.setWechat(i.getWechat());
                int character=i.getJuese();
                if(character==1){
                    iv.setCharacter("管理员");
                }else{
                    iv.setCharacter("普通用户");
                }
                iv.setAccount(i.getAccount());
                iv.setPassword(i.getPassword());
                lv.add(iv);
                j++;
            }
            pageInfo=new PageInfo<>(lv);
            plist.addAll(pageInfo.getList());
            request.setAttribute("plist",plist);
        }else if("普通用户".equals(string)){
            PageHelper.startPage(pn==0?1:pn,7);
            List<InformationEntity> list=fun.queryInformation(string);
            PageInfo pageInfo=new PageInfo(list);
            int pages=pageInfo.getPages();
            int pageSize=pageInfo.getPageSize();
            int lastpage=pageInfo.getLastPage();
            int[] navigatepagenums=pageInfo.getNavigatepageNums();
            int pagenum=pageInfo.getPageNum();
            request.setAttribute("pages",pages);
            request.setAttribute("pageSize",pageSize);
            request.setAttribute("pn",pn);
            request.setAttribute("lastpage",lastpage);
            request.setAttribute("pagenum",pagenum);
            request.setAttribute("navigatepagenums",navigatepagenums);
            for(InformationEntity i:list){
                InformationVo iv=new InformationVo();
                iv.setId(i.getId());
                iv.setUsername(i.getUsername());
                String sex=i.getSex();
                iv.setSex(i.getSex());
                iv.setQq(i.getQq());
                iv.setEmail(i.getEmail());
                iv.setWechat(i.getWechat());
                int character=i.getJuese();
                if(character==1){
                    iv.setCharacter("管理员");
                }else{
                    iv.setCharacter("普通用户");
                }
                iv.setAccount(i.getAccount());
                iv.setPassword(i.getPassword());
                lv.add(iv);
                j++;
            }
            pageInfo=new PageInfo<>(lv);
            plist.addAll(pageInfo.getList());
            request.setAttribute("plist",plist);
        }else{
            PageHelper.startPage(pn==0?1:pn,7);
            List<InformationEntity> list=fun.selectLikeInformation(string);
            PageInfo pageInfo=new PageInfo(list);
            int pages=pageInfo.getPages();
            int pageSize=pageInfo.getPageSize();
            int lastpage=pageInfo.getLastPage();
            int[] navigatepagenums=pageInfo.getNavigatepageNums();
            int pagenum=pageInfo.getPageNum();
            request.setAttribute("pages",pages);
            request.setAttribute("pageSize",pageSize);
            request.setAttribute("pn",pn);
            request.setAttribute("lastpage",lastpage);
            request.setAttribute("pagenum",pagenum);
            request.setAttribute("navigatepagenums",navigatepagenums);
            for(InformationEntity i:list){
                InformationVo iv=new InformationVo();
                iv.setId(i.getId());
                iv.setUsername(i.getUsername());
                iv.setSex(i.getSex());
                iv.setQq(i.getQq());
                iv.setEmail(i.getEmail());
                iv.setWechat(i.getWechat());
                int character=i.getJuese();
                if(character==1){
                    iv.setCharacter("管理员");
                }else{
                    iv.setCharacter("普通用户");
                }
                iv.setAccount(i.getAccount());
                iv.setPassword(i.getPassword());
                lv.add(iv);
                j++;
            }
            pageInfo=new PageInfo<>(lv);
            plist.addAll(pageInfo.getList());
            request.setAttribute("plist",plist);
        }
        request.setAttribute("string",string);
        return null;
        //return "Information/qry";
    }

    @RequestMapping(path = "/Information/qry.do",method= RequestMethod.POST)
    public String qry(int pn, String string,HttpServletRequest request) throws Exception{
        try{
            ArrayList<InformationVo> lv=new ArrayList<>();
            List<InformationVo> plist=new ArrayList<>();
            if("管理员".equals(string)){
                PageHelper.startPage(pn==0?1:pn,7);
                List<InformationEntity> list=fun.queryInformation(string);
                PageInfo pageInfo=new PageInfo(list);
                int pages=pageInfo.getPages();
                int pageSize=pageInfo.getPageSize();
                int lastpage=pageInfo.getLastPage();
                int[] navigatepagenums=pageInfo.getNavigatepageNums();
                int pagenum=pageInfo.getPageNum();
                request.setAttribute("pages",pages);
                request.setAttribute("pageSize",pageSize);
                request.setAttribute("pn",pn);
                request.setAttribute("lastpage",lastpage);
                request.setAttribute("pagenum",pagenum);
                request.setAttribute("navigatepagenums",navigatepagenums);
                for(InformationEntity i:list){
                    InformationVo iv=new InformationVo();
                    iv.setId(i.getId());
                    iv.setUsername(i.getUsername());
                    iv.setSex(i.getSex());
                    iv.setQq(i.getQq());
                    iv.setEmail(i.getEmail());
                    iv.setWechat(i.getWechat());
                    int character=i.getJuese();
                    if(character==1){
                        iv.setCharacter("管理员");
                    }else{
                        iv.setCharacter("普通用户");
                    }
                    iv.setAccount(i.getAccount());
                    iv.setPassword(i.getPassword());
                    lv.add(iv);
                }
                pageInfo=new PageInfo<>(lv);
                plist.addAll(pageInfo.getList());
                request.setAttribute("plist",plist);
            }else if("普通用户".equals(string)){
                PageHelper.startPage(pn==0?1:pn,7);
                List<InformationEntity> list=fun.queryInformation(string);
                PageInfo pageInfo=new PageInfo(list);
                int pages=pageInfo.getPages();
                int pageSize=pageInfo.getPageSize();
                int lastpage=pageInfo.getLastPage();
                int[] navigatepagenums=pageInfo.getNavigatepageNums();
                int pagenum=pageInfo.getPageNum();
                request.setAttribute("pages",pages);
                request.setAttribute("pageSize",pageSize);
                request.setAttribute("pn",pn);
                request.setAttribute("lastpage",lastpage);
                request.setAttribute("pagenum",pagenum);
                request.setAttribute("navigatepagenums",navigatepagenums);
                for(InformationEntity i:list){
                    InformationVo iv=new InformationVo();
                    iv.setId(i.getId());
                    iv.setUsername(i.getUsername());
                    iv.setSex(i.getSex());
                    iv.setQq(i.getQq());
                    iv.setEmail(i.getEmail());
                    iv.setWechat(i.getWechat());
                    int character=i.getJuese();
                    if(character==1){
                        iv.setCharacter("管理员");
                    }else{
                        iv.setCharacter("普通用户");
                    }
                    iv.setAccount(i.getAccount());
                    iv.setPassword(i.getPassword());
                    lv.add(iv);
                }
                pageInfo=new PageInfo<>(lv);
                plist.addAll(pageInfo.getList());
                request.setAttribute("plist",plist);
            }else{
                PageHelper.startPage(pn==0?1:pn,7);
                List<InformationEntity> list=fun.selectLikeInformation(string);
                PageInfo pageInfo=new PageInfo(list);
                int pages=pageInfo.getPages();
                int pageSize=pageInfo.getPageSize();
                int lastpage=pageInfo.getLastPage();
                int[] navigatepagenums=pageInfo.getNavigatepageNums();
                int pagenum=pageInfo.getPageNum();
                request.setAttribute("pages",pages);
                request.setAttribute("pageSize",pageSize);
                request.setAttribute("pn",pn);
                request.setAttribute("lastpage",lastpage);
                request.setAttribute("pagenum",pagenum);
                request.setAttribute("navigatepagenums",navigatepagenums);
                for(InformationEntity i:list){
                    InformationVo iv=new InformationVo();
                    iv.setId(i.getId());
                    iv.setUsername(i.getUsername());
                    iv.setSex(i.getSex());
                    iv.setQq(i.getQq());
                    iv.setEmail(i.getEmail());
                    iv.setWechat(i.getWechat());
                    int character=i.getJuese();
                    if(character==1){
                        iv.setCharacter("管理员");
                    }else{
                        iv.setCharacter("普通用户");
                    }
                    iv.setAccount(i.getAccount());
                    iv.setPassword(i.getPassword());
                    lv.add(iv);
                }
                pageInfo=new PageInfo<>(lv);
                plist.addAll(pageInfo.getList());
                request.setAttribute("plist",plist);
            }
            request.setAttribute("string",string);
        }catch (ThisSystemException e){
        }
        return "Information/qry";
    }

    @RequestMapping(path = "/Information/add.do",method= RequestMethod.GET)
    public String addView() throws Exception{
        return "Information/add";
    }

    @RequestMapping(path = "/Information/add.do",method= RequestMethod.POST)
    public String add(int pn,InformationAo ia,HttpServletRequest request) throws Exception{
        try{
            //System.out.println("password:"+ia.getPassword()+"newpassword"+ia.getNewpassword());
            fun.addInformation(ia);
            PageHelper.startPage(pn==0?1:pn,7);
            List<InformationEntity> slist=fun.selectAllInformation();
            PageInfo pageInfo=new PageInfo(slist);
            int pages=pageInfo.getPages();
            int pageSize=pageInfo.getPageSize();
            //int nextpage=pageInfo.getNextPage();
            int lastpage=pageInfo.getLastPage();
            int[] navigatepagenums=pageInfo.getNavigatepageNums();
            int pagenum=pageInfo.getPageNum();
            request.setAttribute("pages",pages);
            request.setAttribute("pageSize",pageSize);
            request.setAttribute("pn",pn);
            request.setAttribute("lastpage",lastpage);
            request.setAttribute("pagenum",pagenum);
            request.setAttribute("navigatepagenums",navigatepagenums);
            ArrayList<InformationVo> al=new ArrayList<>();
            List<InformationVo> plist=new ArrayList<>();
            int j=0;
            for(InformationEntity i:slist){
                InformationVo iv=new InformationVo();
                iv.setId(i.getId());
                iv.setUsername(i.getUsername());
                iv.setSex(i.getSex());
                iv.setQq(i.getQq());
                iv.setEmail(i.getEmail());
                iv.setWechat(i.getWechat());
                if(i.getCharacter()==1){
                    iv.setCharacter("管理员");
                }else{
                    iv.setCharacter("普通用户");
                }
                iv.setAccount(i.getAccount());
                iv.setPassword(i.getPassword());
                al.add(iv);
            }
            pageInfo=new PageInfo<>(al);
            plist.addAll(pageInfo.getList());
            request.setAttribute("plist",plist);
            request.setAttribute("message","添加成功");
            //跳转到
            return "Information/selectallinformation";
        }catch(ThisSystemException e){
            request.setAttribute("message",e.getMessage());
        }
        return "Information/add";
    }

    @RequestMapping(path = "/Information/delect.do",method= RequestMethod.GET)
    public String delect(int pn,String id,HttpServletRequest request,Map model) throws Exception{
        try{
            fun.delectInformation(id);PageHelper.startPage(pn==0?1:pn,7);
            List<InformationEntity> list=fun.selectAllInformation();
            PageInfo pageInfo=new PageInfo(list);
            int pages=pageInfo.getPages();
            int pageSize=pageInfo.getPageSize();
            //int nextpage=pageInfo.getNextPage();
            int lastpage=pageInfo.getLastPage();
            int[] navigatepagenums=pageInfo.getNavigatepageNums();
            int pagenum=pageInfo.getPageNum();
            request.setAttribute("pages",pages);
            request.setAttribute("pageSize",pageSize);
            request.setAttribute("pn",pn);
            request.setAttribute("lastpage",lastpage);
            request.setAttribute("pagenum",pagenum);
            request.setAttribute("navigatepagenums",navigatepagenums);
            ArrayList<InformationVo> al=new ArrayList<>();
            List<InformationVo> plist=new ArrayList<>();
            int j=0;
            for(InformationEntity i:list){
                InformationVo iv=new InformationVo();
                iv.setId(i.getId());
                iv.setUsername(i.getUsername());
                iv.setSex(i.getSex());
                iv.setQq(i.getQq());
                iv.setEmail(i.getEmail());
                iv.setWechat(i.getWechat());
                if(i.getCharacter()==1){
                    iv.setCharacter("管理员");
                }else{
                    iv.setCharacter("普通用户");
                }
                iv.setAccount(i.getAccount());
                iv.setPassword(i.getPassword());
                al.add(iv);
            }
            pageInfo=new PageInfo<>(al);
            plist.addAll(pageInfo.getList());
            request.setAttribute("plist",plist);///
            request.setAttribute("message","删除成功");
            //return "welcome";
        }catch (ThisSystemException e){
            request.setAttribute("message",e.getMessage());
        }
        return "Information/selectallinformation";
    }

    @RequestMapping(path = "/Information/userinformation.do",method= RequestMethod.GET)
    public String uiView(HttpServletRequest request,Map model) throws Exception{
        HttpSession session=request.getSession();
        InformationEntity ie=(InformationEntity) session.getAttribute("currentUser");
        try{
            InformationEntity i=fun.showInformation(ie.getId());
            InformationVo iv=new InformationVo();
            iv.setId(i.getId());
            iv.setUsername(i.getUsername());
            iv.setSex(i.getSex());
            iv.setQq(i.getQq());
            iv.setEmail(i.getEmail());
            iv.setWechat(i.getWechat());
            int character=i.getJuese();
            if(character==1){
                iv.setCharacter("管理员");
            }else{
                iv.setCharacter("普通用户");
            }
            iv.setAccount(i.getAccount());
            iv.setPassword(i.getPassword());
            model.put("i",iv);
        }catch(ThisSystemException e){
        }
        return "Information/userinformation";
    }

    @RequestMapping(path = "/Information/updateinformation.do",method= RequestMethod.GET)
    public String updateInformationView(String id,Map model,HttpServletRequest request) throws Exception{
        try{
            InformationEntity i=fun.select(id);
            InformationAo ia=new InformationAo();
            ia.setId(id);
            model.put("param",ia);
        }catch(ThisSystemException e){
        }
        return "Information/updateinformation";
    }

    @RequestMapping(path = "/Information/updateinformation.do",method= RequestMethod.POST)
    public String updateInformation(int pn,String id,InformationAo ia,HttpServletRequest request) throws Exception{
        List<InformationVo> plist=new ArrayList<>();
        try{
            if("1".equals(ia.getSex())){
                ia.setSex("男");
            }else{
                ia.setSex("女");
            }
            fun.updateInformation(id,ia);
            PageHelper.startPage(pn==0?1:pn,7);
            List<InformationEntity> list=fun.selectAllInformation();
            PageInfo pageInfo=new PageInfo(list);
            int pages=pageInfo.getPages();
            int pageSize=pageInfo.getPageSize();
            int lastpage=pageInfo.getLastPage();
            int[] navigatepagenums=pageInfo.getNavigatepageNums();
            int pagenum=pageInfo.getPageNum();
            request.setAttribute("pages",pages);
            request.setAttribute("pageSize",pageSize);
            request.setAttribute("pn",pn);
            request.setAttribute("lastpage",lastpage);
            request.setAttribute("pagenum",pagenum);
            request.setAttribute("navigatepagenums",navigatepagenums);
            ArrayList<InformationVo> al=new ArrayList<>();
            int j=0;
            for(InformationEntity i:list){
                InformationVo iv=new InformationVo();
                iv.setId(i.getId());
                iv.setUsername(i.getUsername());
                iv.setSex(i.getSex());
                iv.setQq(i.getQq());
                iv.setEmail(i.getEmail());
                iv.setWechat(i.getWechat());
                if(i.getCharacter()==1){
                    iv.setCharacter("管理员");
                }else{
                    iv.setCharacter("普通用户");
                }
                iv.setAccount(i.getAccount());
                iv.setPassword(i.getPassword());
                al.add(iv);
            }
            pageInfo=new PageInfo<>(al);
            plist.addAll(pageInfo.getList());
            request.setAttribute("plist",plist);
            request.setAttribute("message","修改成功");
        }catch(ThisSystemException e){
            request.setAttribute("message", e.getMessage());
            return "Information/updateinformation";
        }
        return "Information/selectallinformation";
    }

    @RequestMapping(path = "/Information/selectallinformation.do",method= RequestMethod.GET)
    public String selectallinformationView(int pn,HttpServletRequest request) throws Exception{
        PageHelper.startPage(pn==0?1:pn,7);
        List<InformationEntity> list=fun.selectAllInformation();
        PageInfo pageInfo=new PageInfo(list);
        int pages=pageInfo.getPages();
        int pageSize=pageInfo.getPageSize();
        int lastpage=pageInfo.getLastPage();
        int[] navigatepagenums=pageInfo.getNavigatepageNums();
        int pagenum=pageInfo.getPageNum();
        request.setAttribute("pages",pages);
        request.setAttribute("pageSize",pageSize);
        request.setAttribute("pn",pn);
        request.setAttribute("lastpage",lastpage);
        request.setAttribute("pagenum",pagenum);
        request.setAttribute("navigatepagenums",navigatepagenums);
        ArrayList<InformationVo> al=new ArrayList<>();
        List<InformationVo> plist=new ArrayList<>();
        for(InformationEntity i:list){
            InformationVo iv=new InformationVo();
            iv.setId(i.getId());
            iv.setUsername(i.getUsername());
            iv.setSex(i.getSex());
            iv.setQq(i.getQq());
            iv.setEmail(i.getEmail());
            iv.setWechat(i.getWechat());
            if(i.getCharacter()==1){
                iv.setCharacter("管理员");
            }else{
                iv.setCharacter("普通用户");
            }
            iv.setAccount(i.getAccount());
            iv.setPassword(i.getPassword());
            al.add(iv);
        }
        pageInfo=new PageInfo(al);
        plist.addAll(pageInfo.getList());
        request.setAttribute("plist",plist);
        return null;
    }

    @RequestMapping(path="/Information/updatepassword.do",method= RequestMethod.GET)
    public String updatePasswordView()throws Exception{
        return "Information/updatepassword";
    }

    @RequestMapping(path="/Information/updatepassword.do",method= RequestMethod.POST)
    public String updatePassword(String id,InformationAo ia,HttpServletRequest request)throws Exception{
        InformationEntity i=fun.select(id);
        String password=$("密码必须填写",ia.getPassword());
        String newpassword=$("请重新填写密码",ia.getNewpassword());
        assertEquals("两次密码不一致",ia.getPassword(),ia.getNewpassword());
        assertEquals("原密码密码不一致",i.getPassword(),ia.getNewpassword());
        return null;
    }
}
